﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmschedulecx : Form
    {
        public Frmschedulecx()
        {
            InitializeComponent();
        }
        public Frmschedulecx(string flag)
        {
            InitializeComponent();
            if (flag == "mod")
            {//修改按钮显示
                this.btnMod.Visible = true;
            }
            else if (flag == "del")
            {//删除按钮显示
                this.BtnDel.Visible = true;
            }
            try
            {
                DataSet dsClass = CoursescheduleOperation.getCourseschedule(new CoursescheduleData());
                this.combClassid.DataSource = dsClass.Tables[0];
                this.combClassid.ValueMember = "班级名称";
                this.combClassid.DisplayMember = "班级名称";
                this.combKcid.DataSource = dsClass.Tables[0];
                this.combKcid.ValueMember = "课程编号";
                this.combKcid.DisplayMember = "课程编号";
                DataSet dsTea = TeacherInfoOperation.getTeacherInfo(new TeacherInfoData());
                this.combTeaid.DataSource = dsTea.Tables[0];
                this.combTeaid.ValueMember = "教师编号";
                this.combTeaid.DisplayMember = "教师姓名";
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

        }
        public void bindDataGrid()
        {
            string termid = this.txtTermid.Text.Trim();
            string classid = this.combClassid.Text.Trim();
            string kcid = (string)this.combKcid.Text.Trim();
            string teaid = (string)this.combTeaid.Text.Trim();
            CoursescheduleData data = new CoursescheduleData();
            data.Termid = termid; data.Classid = classid;
            data.Kcid = kcid; data.Teaid = teaid;
            try
            {
                 DataSet ds = CoursescheduleOperation.getCourseschedule(data);
                this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindDataGrid();
        }

        private void btnMod_Click(object sender, EventArgs e)
        {

            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择要修改的记录
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            else
            {
                string termid = this.txtTermid.Text.Trim();
                string classid = this.combClassid.SelectedText.Trim();
                string kcid = (string)this.combKcid.SelectedValue;
                string teaid = (string)this.combTeaid.Text;
                CoursescheduleData data = new CoursescheduleData();
                data.Termid = termid; data.Classid = classid;
                data.Kcid = kcid; data.Teaid = teaid;
                DataSet ds = CoursescheduleOperation.getCourseschedule(data);
                string termid1 = ds.Tables[0].Rows[index]["学期"].ToString();
                string classid1= ds.Tables[0].Rows[index]["班级名称"].ToString();
                string kcid1 = ds.Tables[0].Rows[index]["课程编号"].ToString();
                string teaid1=ds.Tables[0].Rows[index]["教师姓名"].ToString();
                Frmaddschedule modsche = new Frmaddschedule(termid1, classid1, kcid1,teaid1);
                modsche.ShowDialog(this);
            }
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {

            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择要删除的记录
                MessageBox.Show("请选择要删除的记录！", "提示");
                return;
            }
            else
            {
                string termid = this.txtTermid.Text.Trim();
                string classid = this.combClassid.SelectedText.Trim();
                string kcid = (string)this.combKcid.SelectedValue;
                string teaid = (string)this.combTeaid.SelectedValue;
                CoursescheduleData data = new CoursescheduleData();
                data.Termid = termid; data.Classid = classid;
                data.Kcid = kcid; data.Teaid = teaid;
                DataSet ds = CoursescheduleOperation.getCourseschedule(data);
                if(MessageBox.Show("确定要删除吗","删除",MessageBoxButtons.YesNo)==DialogResult.Yes)
                {
                    string termid1 = ds.Tables[0].Rows[index]["学期"].ToString();
                    string classid1 = ds.Tables[0].Rows[index]["班级名称"].ToString();
                    string kcid1 = ds.Tables[0].Rows[index]["课程编号"].ToString();
                    try
                    {
                        CoursescheduleData data1 = new CoursescheduleData();
                        data1.Termid = termid1;
                        data1.Classid = classid1;
                        data1.Kcid = kcid1;
                        if (CoursescheduleOperation.deleteCourseschedule(data1))
                        {
                            MessageBox.Show("删除成功!", "提示");
                            bindDataGrid();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！","错误");
                        }
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！","错误");
                    }
                }
            }
        }
    }
    public class CoursescheduleData
    {
        private string termid = "";
        private string classid = "";
        private string kcid = "";
        private string teaid = "";
        public string Termid//学期代码
        {
            get { return termid; }
            set { termid = value; }
        }
        public string Classid//班级名称
        {
            get { return classid; }
            set { classid = value; }
        }
        public string Kcid//课程编号
        {
            get { return kcid; }
            set { kcid = value; }
        }
        public string Teaid//教师编号
        {
            get { return teaid; }
            set { teaid = value; }
        }
    }
    public class CoursescheduleOperation
    {
        private static DataAccess dataAccess = new DataAccess();
        public static bool insertCourseschedule(CoursescheduleData data)
        {//数据插入
            string sql = "insert into 课程表(学期,班级名称,课程编号,教师编号) values(" +
                "'" + data.Termid + "'," +
                 "'" + data.Classid + "'," +
                  "'" + data.Kcid + "'," +
                   "'" + data.Teaid + "')";
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updateCourseschedule(CoursescheduleData data)
        {//数据修改
            string sql = "update 课程表 set 教师编号='" + data.Teaid + "'" +
                "where 学期='" + data.Termid + "'and " +
                "班级名称='" + data.Classid + "'and " +
                "课程编号='" + data.Kcid + "'";
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool deleteCourseschedule(CoursescheduleData data)
        {//数据删除
            string sql = String.Format("delete 课程表 where 学期='{0}'and 班级名称='{1}' and 课程编号='{2}'", data.Termid,
                data.Classid, data.Kcid);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static DataSet getCourseschedule(CoursescheduleData data)
        {//数据查询
            string condition = "";//查询的条件
            if(data.Termid!=null&& data.Termid!="")
            {
                condition += " and a.学期='" + data.Termid + "'";
            }
            if(data.Classid!=null&&data.Classid!="")
            {
                condition += " and a.班级名称 like '%" + data.Classid + "%'";
            }
            if(data.Kcid!=null&&""!=data.Kcid)
            {
                condition += " and a.课程编号='" + data.Kcid + "'";
            }
            if(data.Teaid!=null && data.Teaid!="")
            {
                condition += " and c.教师姓名='" + data.Teaid + "'";
            }
            string sql = "select a.学期,a.班级名称,b.课程编号,b.课程名称,c.教师姓名,a.教师编号"
            + " from 课程表 a, 课程信息表 b,教师信息表 c where a.课程编号=b.课程编号 and a.教师编号=c.教师编号" + condition;
            return dataAccess.GetDataSet(sql, "课程表");//执行sql语句
        }
    }
}